/**
 * 
 */
package pt.inescid.components.appadaptationlayer.applications.povray;

/**
 * @author Sergio Esteves (sesteves at gsd.inesc-id.pt)
 *
 */
public class CombinePPM {

	private static int getHeaderIdx(byte[] ppm) {
		int countLF = 0;
		for(int i = 0; i < ppm.length; i++)
			if(ppm[i] == '\n')
				if(++countLF == 3) 
					return i + 1;
		return -1;
	}
	
	public static byte[] combine(byte[] firstPPM, byte[] tailPPM) {				
		int srcPos = getHeaderIdx(tailPPM);
		int tailUtilSize = tailPPM.length - srcPos;
		byte[] result = new byte[firstPPM.length + tailUtilSize];
		System.arraycopy(firstPPM, 0, result, 0, firstPPM.length);
		System.arraycopy(tailPPM, srcPos, result, firstPPM.length, tailUtilSize);		
		return result;
	}		
}
